import { getUserInfo } from '@/api/user'
import router from '@/router'
import type { UserInfo } from '@/types/user'
import { message } from '@/uitls/message'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
    const user = ref('')

    async function login(userInfo: UserInfo) {
        const res = await getUserInfo(userInfo)
        if (res.data.code != 100) {
            message('error', res.data.desc)
            return false
        }
        user.value = res.data.data.token
        localStorage.setItem('X-Auth-Token', user.value)
        return true
    }

    function logout() {
        user.value = ''
        localStorage.removeItem('X-Auth-Token')
        router.push('/login')
    }
    return { user, login, logout }
})
